import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:szwf/utils/dio_http.dart';
import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:szwf/utils/common_toast.dart';
class LoginPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset:false,
      body: (
          Stack(
            alignment: Alignment.center,
            children: <Widget>[
              Container(
//                  width: double.infinity,
                  height:double.infinity,
                  decoration: BoxDecoration(

                    image:  DecorationImage(
                      image:  AssetImage('assets/Bg.png'),
                    ),

                  )
              ),
              Positioned(

                child:Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('苏州市高新区',style: TextStyle(color: Colors.white,fontSize: 24.0),),
                    Text(' 固危平衡试点项目',style: TextStyle(color: Colors.white,fontSize: 24.0),)
                  ],
                ) ,
                top: 130.0,

              ),
              Positioned(
                child:Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('企业端',style: TextStyle(color: Colors.white,fontSize: 14.0),),
                  ],
                ) ,
                top: 170.0,
                right: 40.0,
              ),
              Positioned(
                child:Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text('版权所有：苏州市伏泰信息科技股份有限公司',style: TextStyle(color: Colors.grey,fontSize: 14.0),),
                  ],
                ) ,
                bottom: 70.0,

              ),
              Align(
                alignment: Alignment(0.2,0.7),
                child: Container(
                    width: double.infinity,
                    height: 300.0,
                    child:Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        LoginFrom()
                      ],
                    ) ,
                ),
              ),

            ],
          )

        )
      );

//      Container(
//      child: PageContent(name:'登录页'),
//    );
  }
}

class LoginFrom extends StatefulWidget {
  @override
  _LoginFromState createState() => _LoginFromState();
}

class _LoginFromState extends State<LoginFrom> {
  var name = TextEditingController();
  var password = TextEditingController();
  // md5 加密
  String generateMd5(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return hex.encode(digest.bytes);
  }
  onLogin() async {

    const url = '/wfgl/login/login';
    var params = {
      "loginName":name.text,
      "passWord":generateMd5(password.text),
      "noVerifyCode":'false'
    };
    var res = await DioHttp.of(context).post(url,params);
    var resString = json.decode(res.toString());

    if(resString['rc'] == 0){
//      print('正确');
      CommonToast.showToast('登录成功');
//      String token = resString['ret']['access_token'];
      String companyId = resString['ret']['companyId'].toString();
      String companyName = resString['ret']['companyName'].toString();

        final prefs = await SharedPreferences.getInstance();
        //setValue
        prefs.setString('companyId', companyId);
      prefs.setString('companyName', companyName);

      Navigator.of(context).pushReplacementNamed('/');
    }else{
//      print('正确');
      CommonToast.showToast(resString['err']);
    }

  }
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(40.0),
      child:  Form(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            TextFormField(
              controller: name,
              decoration: const InputDecoration(
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.blue),
                  ),
                  enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                prefixIcon: Icon(Icons.supervised_user_circle,color: Colors.blue,),
                  hintText: '请填写用户名',
                  hintStyle: TextStyle(
                      fontSize: 12.0,
                      color: Colors.grey
                  )
              ),
              validator: (value) {
                if (value.isEmpty) {
                  return '用户名为必填参数!';
                }
                return null;
              },
            ),
            TextFormField(
              obscureText: true,
              controller: password,
              decoration: const InputDecoration(
                  focusedBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.blue),
                  ),
                  enabledBorder: UnderlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                prefixIcon: Icon(Icons.lock_open,color: Colors.blue),
                hintText: '请填写密码',
                hintStyle: TextStyle(
                  fontSize: 12.0,
                  color: Colors.grey
                )

              ),
              validator: (value) {
                if (value.isEmpty) {
                  return '密码为必填参数!';
                }
                return null;
              },
            ),
            SizedBox(
              height: 20.0,
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: Container(
                    height: 35,
                    child: RaisedButton(
                      color: Colors.blue,
                      textColor: Colors.white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(25),
                      ),
                      onPressed: () {
                        // Validate will return true if the form is valid, or false if
                        // the form is invalid.
                        if (_formKey.currentState.validate()) {
                            onLogin();
                          // Process data.
                        }
                      },
                      child: Text('登录'),
                    ),
                  ),
                )
              ],
            ),

          ],
        ),
      ),
    );
  }
}

